package com.xmg.p2p.base.service.impl;

import com.xmg.p2p.base.domain.SystemDictionaryItem;
import com.xmg.p2p.base.domain.UserFile;
import com.xmg.p2p.base.domain.Userinfo;
import com.xmg.p2p.base.mapper.UserFileMapper;
import com.xmg.p2p.base.query.PageResult;
import com.xmg.p2p.base.query.UserFileQueryObject;
import com.xmg.p2p.base.service.UserFileService;
import com.xmg.p2p.base.service.UserinfoService;
import com.xmg.p2p.base.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserFileServiceImpl implements UserFileService {

    @Autowired
    private UserFileMapper userFileMapper;
    @Autowired
    private UserinfoService userinfoService;

    /**
     * 根据用户是否选择了风控材料分类来获取对应的风控材料对象信息
     *
     * @return 参数为 false 返回 没有选择风控分类的 信息
     * 参数为 true 返回 选了风控分类的 信息
     */
    public List<UserFile> listUserFilesHasSelectType(boolean hasType) {
        return userFileMapper.listUserFilesHasSelectType(UserContext.getCurrent().getId(), hasType);
    }

    /**
     * 处理上传用户风控文件
     */
    public void apply(String fileName) {
        UserFile uf = new UserFile();
        uf.setApplier(UserContext.getCurrent()); //保存当前的申请人
        uf.setApplyTime(new Date());
        uf.setImage(fileName);
        uf.setState(UserFile.STATE_NORMAL); //保存当前材料的状态  待审核
        this.userFileMapper.insert(uf);
    }

    /**
     * 处理用户风控文件类型选择
     */
    //这里传进来的fileTypes[i] 是对应的 的数据字典明细对应的id
    //ids 是 user file对象的id  file types：是数据字典类型id
    public void selectTypes(Long[] ids, Long[] fileTypes) {
        if (ids != null && fileTypes != null && ids.length == fileTypes.length) {
            for (int i = 0; i < fileTypes.length; i++) {
                //查出当前 ids[i] 对应的user file对象
                UserFile uf = this.userFileMapper.selectByPrimaryKey(ids[i]);
                if (uf.getFileType() == null) {
                    SystemDictionaryItem item = new SystemDictionaryItem();
                    item.setId(fileTypes[i]); //设置对应的类型id(将此id保存到数据库中)
                    uf.setFileType(item);
                    this.userFileMapper.updateByPrimaryKey(uf);
                }
            }
        }
    }

    public PageResult query(UserFileQueryObject qo) {
        int count = this.userFileMapper.queryForCount(qo);
        if (count > 0) {
            List<UserFile> list = this.userFileMapper.query(qo);
            return new PageResult(list, count, qo.getCurrentPage(), qo.getPageSize());
        }

        return PageResult.empty(qo.getPageSize());
    }

    public void aduit(Long id, String remark, int score, int state) {
        //先的到当前的userFile对象,判断状态
        UserFile uf = this.userFileMapper.selectByPrimaryKey(id);
        if (uf != null && uf.getState() == UserFile.STATE_NORMAL) { //表示不为空且处于带审核状态
            //设置相关的属性
            uf.setAuditor(UserContext.getCurrent());
            uf.setAuditTime(new Date());
            uf.setRemark(remark);
            uf.setState(state);
            //如果审核通过 累加用户的风控分数
            if (state == UserFile.STATE_AUDIT) {
                uf.setScore(score);
                Userinfo userinfo = this.userinfoService.getById(uf.getApplier().getId());
                userinfo.setAuthScore(userinfo.getAuthScore()+score);
                this.userinfoService.update(userinfo);
            }
            this.userFileMapper.updateByPrimaryKey(uf);
        }
    }
    /**
     * 根据条件获取对应 风控材料
     * @param qo
     * @return
     */
    public List<UserFile> queryForList(UserFileQueryObject qo) {
        return this.userFileMapper.query(qo);
    }
}
